package com.chenxin.wms.controller;

import com.chenxin.common.core.controller.BaseController;
import com.chenxin.common.core.domain.AjaxResult;
import com.chenxin.common.exception.ServiceException;
import com.chenxin.wms.domain.dto.BatchForbidRequestDTO;
import com.chenxin.wms.process.check.ICheckProcess;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 盘点业务（盘点创建、执行、取消、确认）
 */
@RestController
@RequestMapping("/wms/check")
public class CheckController extends BaseController {

    @Autowired
    private ICheckProcess checkProcess;

    @PostMapping("create")
    public AjaxResult check(@RequestBody BatchForbidRequestDTO request) {
        request.setUserId(this.getUserId());
        try {
            checkProcess.stockCheck(request);
            //处理结果
            return AjaxResult.success("创建成功");
        } catch (Exception e) {
            return AjaxResult.error("操作失败" + e.getMessage());
        }
    }

    @PostMapping("execute")
    public AjaxResult outbound(@RequestBody BatchForbidRequestDTO request) {
        List<Long> taskIds = request.getIds();
        request.setUserId(this.getUserId());
        if (ObjectUtils.isEmpty(taskIds)) {
            throw new ServiceException("未选择待出库任务");
        }
        try {
            checkProcess.outbound(request);
            //处理结果
            return AjaxResult.success("盘点任务执行成功");
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("操作失败" + e.getMessage());
        }
    }

    @PostMapping("cancel")
    public AjaxResult cancel(@RequestBody BatchForbidRequestDTO request) {
        List<Long> taskIds = request.getIds();
        if (ObjectUtils.isEmpty(taskIds)) {
            throw new ServiceException("未选择待取消任务");
        }
        try {
            checkProcess.cancel(request);
            //处理结果
            return AjaxResult.success("取消成功");
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("操作失败" + e.getMessage());
        }
    }

    @PostMapping("confirm")
    public AjaxResult confirm(@RequestBody BatchForbidRequestDTO request) {
        return AjaxResult.success("盘点任务确认成功");
    }
}
